<!--
 * @Author: 噜噜雯雯 2199987619@qq.com
 * @Date: 2025-06-21 16:36:53
 * @LastEditors: 噜噜雯雯 2199987619@qq.com
 * @LastEditTime: 2025-09-15 15:00:58
 * @FilePath: \linePatrol\vue-element-plus-admin\src\views\EquipManage\EquipType\index.vue
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<script setup lang="tsx">
import { ContentWrap } from '@/components/ContentWrap'
import { useI18n } from '@/hooks/web/useI18n'
import { Table } from '@/components/Table'
import { ref, unref, reactive } from 'vue'
import { useTable } from '@/hooks/web/useTable'
import { CrudSchema, useCrudSchemas } from '@/hooks/web/useCrudSchemas'
import { BaseButton } from '@/components/Button'
import { Dialog } from '@/components/Dialog'
import { FloorChart, FloorLedger } from '@/api/securcheck/type'
import { Search } from '@/components/Search'
import SecurCheckFloorApi from '@/api/securcheck/FloorLedger'
import Write from './components/write.vue'
import BuildingMap from './components/BuildingMap.vue'
import DeptApi from '@/api/dept'
import DrawDataVue from './components/DrawData.vue'
const { t } = useI18n()
const FormDisabled = ref(true)
const actionType = ref()
const dialogTitle = ref('')
const ids = ref<string[]>([])
//获取字典

//表格内容接口
const { tableRegister, tableState, tableMethods } = useTable({
  fetchDataApi: async () => {
    const { pageSize, currentPage } = tableState
    const i = await SecurCheckFloorApi.GetFloorLedger({
      pageIndex: unref(currentPage),
      pageSize: unref(pageSize),
      ...unref(searchParams)
    })
    return {
      list: i.rows,
      total: i.total
    }
  },
  fetchDelApi: async () => {
    const res = await SecurCheckFloorApi.DeletFloorLedger(unref(ids)[0])
    return !!res
  }
})
const { total, loading, dataList, pageSize, currentPage } = tableState
const { getList, getElTableExpose, delList } = tableMethods
//表格结构
const saveLoading = ref(false)
const crudSchemas = reactive<CrudSchema[]>([
  {
    field: 'selection',
    type: 'selection',
    form: {
      hidden: true
    },
    detail: {
      hidden: true
    },
    fixed: 'left',
    search: { hidden: true }
  },
  {
    field: 'index',
    label: t('tableDemo.index'),
    fixed: 'left',
    type: 'index',
    search: {
      hidden: true
    },
    form: {
      hidden: true
    },
    detail: {
      hidden: true
    }
  },
  {
    field: 'floorName',
    label: t('楼宇名称'),
    search: {
      hidden: false,
      component: 'Input',
      componentProps: {
        placeholder: '楼宇名称'
      }
    },
    form: {
      colProps: {
        span: 12
      }
    }
  },
  {
    field: 'deptId',
    label: t('所属公司'),
    width: 200,
    table: {
      hidden: true
    },
    search: {
      hidden: true
    },
    form: {
      colProps: {
        span: 12
      },
      component: 'TreeSelect',
      componentProps: {
        renderAfterExpand: false,
        showCheckbox: true,
        checkStrictly: true,
        checkOnClickNode: true,
        nodeKey: 'id',
        props: {
          label: 'label'
        }
      },
      optionApi: async () => {
        const res = await DeptApi.GetDeptListTree()
        return res.data
      }
    }
  },
  {
    field: 'aliasOne',
    label: t('别名1'),
    search: {
      hidden: true
    },
    form: {
      colProps: {
        span: 12
      }
    }
  },
  {
    field: 'aliasTwo',
    label: t('别名2'),
    search: {
      hidden: true
    },
    form: {
      colProps: {
        span: 12
      }
    }
  },
  {
    field: 'community',
    label: t('小区'),
    search: {
      hidden: true
    },
    table: {
      hidden: true
    },
    form: {
      colProps: {
        span: 12
      }
    }
  },
  {
    field: 'floorNo',
    label: t('楼号'),
    search: {
      hidden: true
    },
    table: {
      hidden: true
    },
    form: {
      colProps: {
        span: 12
      }
    }
  },
  {
    field: 'floorNumber',
    label: t('楼宇编号'),
    search: {
      hidden: true
    },
    form: {
      colProps: {
        span: 12
      }
    }
  },
  {
    field: 'isMap',
    label: t('是否地图定位'),
    search: {
      hidden: true
    },
    formatter: (_: Recordable, __: any, cellValue: number) => {
      return cellValue == 0 ? t('否') : t('是')
    },
    form: {
      colProps: {
        span: 12
      },
      component: 'Select',
      componentProps: {
        options: [
          {
            label: '否',
            value: 0
          },
          {
            label: '是',
            value: 1
          }
        ]
      }
    }
  },
  {
    field: 'lon',
    label: t('经度'),
    search: {
      hidden: true
    },
    table: {
      hidden: true
    },
    form: {
      colProps: {
        span: 12
      }
    }
  },
  {
    field: 'lat',
    label: t('纬度'),
    search: {
      hidden: true
    },
    table: {
      hidden: true
    },
    form: {
      colProps: {
        span: 12
      }
    }
  },
  {
    field: 'address',
    label: t('地址'),
    search: {
      hidden: true
    },
    table: {
      //   hidden: true,
    },
    form: {
      colProps: {
        span: 24
      }
    }
  },
  {
    field: 'remark',
    label: t('备注'),
    search: {
      hidden: true
    },
    table: {
      hidden: true
    },
    form: {
      colProps: {
        span: 24
      }
    }
  },
  {
    field: 'action',
    label: t('tableDemo.action'),
    width: 250,
    fixed: 'right',
    search: {
      hidden: true
    },
    form: {
      hidden: true
    },
    detail: {
      hidden: true
    },
    table: {
      slots: {
        default: (data: any) => {
          return (
            <>
              <span class="TableBtn" onClick={() => action(data.row, 'Detail')}>
                {t('查看')}
              </span>
              <span class="TableBtn" onClick={() => action(data.row, 'Edit')}>
                {t('修改')}
              </span>
              <span class="TableBtn" onClick={() => action(data.row, 'Draw')}>
                {t('绘制')}
              </span>
              <span class="TableBtn" onClick={() => action(data.row, 'BuildingMap')}>
                {t('楼宇图')}
              </span>
              <span class="TableBtn" style="color:#f56c6c" onClick={() => delData(data.row)}>
                {t('删除')}
              </span>
            </>
          )
        }
      }
    }
  }
])
const FloorLedgerRef = ref()
const save = async () => {
  const write = unref(FloorLedgerRef)
  const formData = await write?.submit()
  if (formData) {
    saveLoading.value = true
    saveLoading.value = false
    //新增任务组
    const res = await SecurCheckFloorApi.EditFloorLedger(formData)
      .catch(() => {})
      .finally(() => {
        saveLoading.value = false
      })
    if (res) {
      dialogVisible.value = false
      currentPage.value = 1
      getList()
    }
  }
}
const { allSchemas } = useCrudSchemas(crudSchemas)
const currentRow = ref<FloorLedger>()
const dialogVisible = ref(false)
const delLoading = ref(false)
//表格删除操作
const delData = async (row: FloorLedger | null) => {
  const elTableExpose = await getElTableExpose()
  ids.value = row ? [row.id] : elTableExpose?.getSelectionRows().map((v: any) => v.id) || []
  delLoading.value = true
  await delList(unref(ids).length).finally(() => {
    delLoading.value = false
  })
}
const FloorCharts = ref<FloorChart[]>()

//表格中操作
const action = async (row: FloorLedger, type: string) => {
  actionType.value = type
  if (type == 'Detail') {
    currentRow.value = row
    FormDisabled.value = true
    dialogTitle.value = '查看详情'
  } else if (type == 'Edit') {
    currentRow.value = row
    FormDisabled.value = false
    dialogTitle.value = '修改'
  } else if (type == 'BuildingMap') {
    dialogTitle.value = '楼宇图'
    const res = await SecurCheckFloorApi.GetfloorChart({
      community: row.community,
      floorNo: row.floorNo
    })
    FloorCharts.value = res
  } else {
    currentRow.value = row
    dialogTitle.value = '绘制'
  }
  dialogVisible.value = true //打开弹窗
}
const searchParams = ref({})
const setSearchParams = (params: any) => {
  searchParams.value = params
  getList()
}
</script>
<template>
  <div class="flex w-100% h-100%">
    <ContentWrap class="flex-[3] ml-20px" title="楼宇台账">
      <!-- <BaseButton>{{ t("更新") }}</BaseButton> -->
      <Search
        :schema="allSchemas.searchSchema"
        @search="setSearchParams"
        @reset="setSearchParams"
      />
      <Table
        v-model:current-page="currentPage"
        v-model:page-size="pageSize"
        :columns="allSchemas.tableColumns"
        :data="dataList"
        row-key="id"
        :loading="loading"
        @register="tableRegister"
        :pagination="{
          total
        }"
      />
    </ContentWrap>
    <Dialog
      :maxHeight="actionType == 'BuildingMap' || actionType == 'Draw' ? '500px' : '400px'"
      :style="actionType == 'BuildingMap' || actionType == 'Draw' ? 'width:1400px' : 'width:800px'"
      v-model="dialogVisible"
      :title="dialogTitle"
    >
      <DrawDataVue ref="FloorLedgerRef" :current-row="currentRow" v-if="actionType == 'Draw'" />
      <Write
        v-if="actionType == 'Detail' || actionType == 'Edit'"
        ref="FloorLedgerRef"
        :form-schema="allSchemas.formSchema"
        :current-row="currentRow"
        :FormDisabled="FormDisabled"
      />
      <BuildingMap :current-row="FloorCharts" v-if="actionType == 'BuildingMap'" />
      <template #footer>
        <BaseButton type="primary" :loading="saveLoading" @click="save">
          {{ t('提交') }}
        </BaseButton>
        <BaseButton @click="dialogVisible = false">{{ t('关闭') }}</BaseButton>
      </template>
    </Dialog>
  </div>
</template>
<style scoped>
:deep(.el-radio-button--small .el-radio-button__inner) {
  padding: 7px 15px !important;
}
.el-tree-node:focus > .el-tree-node__content {
  color: #409eff;
}
.el-card__body {
  padding: 10px;
  /* height: 90%; */
}
.CloseBtn {
  font-weight: 600;
  margin: 0 3px;
  color: darkgrey;
}
.AddbtnStyle {
  margin-bottom: 18px;
  margin-right: 10px;
}
.el-collapse-item {
  padding: 15px 0;
}
.el-collapse-item__content {
  padding-bottom: 0;
}
.el-collapse-item__header {
  width: 62px;
  height: 24px;
  border-radius: 2px;
  color: white;
  background: #409eff;
  justify-content: center;
  font-size: 12px;
}
.el-collapse-item__arrow {
  margin: 0 2px;
}

:deep(.TableBtn) {
  margin: 0 5px;
  cursor: pointer;
  color: #409eff;
  font-weight: 600;
}
</style>
